import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;


//VS4E -- DO NOT REMOVE THIS LINE!
public class MainFrame extends JFrame {
	
	private static final long serialVersionUID = 1L;
	java.applet.AudioClip sound;
	java.applet.Applet app;
	URL myUrl;
	
	static GameFrame gameFrame = new GameFrame();
	static CreatePlayerFrame createPlayerFrame = new CreatePlayerFrame();
	static RulesFrame rulesFrame = new RulesFrame();
	static AboutFrame aboutFrame = new AboutFrame();
	static TradingDialog tradingDialog = new TradingDialog();
	static AuctionDialog auctionDialog = new AuctionDialog();
	static WinnerFrame winnerFrame = new WinnerFrame();
	
	private JButton newGameButton;
	private JButton howToPlayButton;
	private JLabel mainFrameBG;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public MainFrame() {
		initComponents();
	}

	private void initComponents() {
		setTitle("Monopoly");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/icon.png")));
		setResizable(false);
		setLayout(new GroupLayout());
		add(newGameButton(), new Constraints(new Leading(280, 10, 10), new Leading(220, 10, 10)));
		add(howToPlayButton(), new Constraints(new Leading(252, 12, 12), new Leading(250, 12, 12)));
		add(mainFrameBG(), new Constraints(new Leading(0, 12, 12), new Leading(0, 12, 12)));
		setSize(400, 300);
	}

	private JLabel mainFrameBG() {
		if (mainFrameBG == null) {
			mainFrameBG = new JLabel();
			mainFrameBG.setIcon(new ImageIcon(getClass().getResource("/images/MainFrameBG2.jpg")));
		}
		return mainFrameBG;
	}

	private JButton newGameButton() {
		if (newGameButton == null) {
			newGameButton = new JButton();
			newGameButton.setText("New Game");
			newGameButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					newGameButtonActionActionPerformed(event);
				}
			});
		}
		return newGameButton;
	}

	private JButton howToPlayButton() {
		if (howToPlayButton == null) {
			howToPlayButton = new JButton();
			howToPlayButton.setText("Monopoly Rules");
			howToPlayButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					howToPlayButtonActionActionPerformed(event);
				}
			});
		}
		return howToPlayButton;
	}

	@SuppressWarnings("unused")
	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String [] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				MainFrame frame = new MainFrame();
				frame.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);
				frame.setTitle("Monopoly");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	@SuppressWarnings("static-access")
	public void makeSound(){
		try {
	        app = new java.applet.Applet(); 
	        sound = app.newAudioClip(myUrl); 
	        }catch(Exception e) {
	        System.out.println(e);
	        } 
	    sound.play();
	}
	
	@SuppressWarnings("static-access")
	public static void startGame(){
		gameFrame.orderedPlayer = createPlayerFrame.orderedPlayer;
		gameFrame.numberOfPlayers = createPlayerFrame.numberOfPlayers;
		gameFrame.startGame();
		gameFrame.setVisible(true);
	}

	public static void declareWinner(){
		gameFrame.setEnabled(false);
		winnerFrame.counter = gameFrame.counter;
		winnerFrame.orderedPlayer = gameFrame.orderedPlayer;
		winnerFrame.loadVisuals();
		winnerFrame.setVisible(true);
	}
	
	public static void startTrade(){
		gameFrame.setEnabled(false);
		gameFrame.disableChooseTradee();
		tradingDialog.trader = gameFrame.counter;
		tradingDialog.tradee = gameFrame.counter2;
		tradingDialog.board = gameFrame.board;
		tradingDialog.orderedPlayer = gameFrame.orderedPlayer;
		tradingDialog.loadVisuals();
		tradingDialog.setVisible(true);
	}

	public static void startAuction(){
		gameFrame.setEnabled(false);
		gameFrame.disableChooseHighestBidder();
		auctionDialog.counter = gameFrame.counter;
		auctionDialog.counter2 = gameFrame.counter2;
		auctionDialog.board = gameFrame.board;
		auctionDialog.orderedPlayer = gameFrame.orderedPlayer;
		auctionDialog.loadVisuals();
		auctionDialog.setVisible(true);
	}
	
	public static void endTrade(){
		gameFrame.updateTextFields();
		gameFrame.resetBabyDeeds();
		gameFrame.updateBabyDeeds();
		gameFrame.updateRoofs();
		gameFrame.checkEnabledButtons();
		gameFrame.setEnabled(true);
		tradingDialog.setVisible(false);
	}
	
	public static void endAuction(){
		gameFrame.showDoubleDice();
		gameFrame.updateTextFields();
		gameFrame.resetBabyDeeds();
		gameFrame.updateBabyDeeds();
		gameFrame.updateRoofs();
		gameFrame.checkEnabledButtons();
		gameFrame.setEnabled(true);
		auctionDialog.setVisible(false);
	}
	
	public static void cancelTrade(){
		gameFrame.checkEnabledButtons();
		gameFrame.setEnabled(true);
		tradingDialog.setVisible(false);
	}
	
	private void newGameButtonActionActionPerformed(ActionEvent event) {
		createPlayerFrame.setVisible(true);
		aboutFrame.dispose();
		this.setVisible(false);
	}

	private void howToPlayButtonActionActionPerformed(ActionEvent event) {
		rulesFrame.setVisible(true);
	}

}
